package com.inspector.app.ui.flight.statusBar;


import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.inspector.app.R;
import com.inspector.app.ui.flight.statusBar.FlightStatusBarPopup.OnItemClickListener;
import com.inspector.app.util.CommonUtil;

import java.util.List;

/**
 * @update 2023/8/23 CodeK
 */
public class MapSetTitleAdapter extends RecyclerView.Adapter<MapSetTitleAdapter.TemplateViewHolder> {

    private final List<FlightStatusBar> list;
    private final Object viewTag;

    public MapSetTitleAdapter(List<FlightStatusBar> list, Object viewTag) {
        this.list = list;
        this.viewTag = viewTag;
    }

    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @NonNull
    @Override
    public TemplateViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_map_set_title, viewGroup, false);
        return new TemplateViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final TemplateViewHolder viewHolder, @SuppressLint("RecyclerView") final int i) {
        Context context = viewHolder.itemView.getContext();
        if (CommonUtil.isTabletDevice(context)){
            viewHolder.tv_title_name.setTextColor(ContextCompat.getColor(context, R.color.white));
            viewHolder.tv_title_name_show.setTextColor(ContextCompat.getColor(context, R.color.white));
        } else {
            viewHolder.tv_title_name.setTextColor(ContextCompat.getColor(context, R.color.colorMapItem));
            viewHolder.tv_title_name_show.setTextColor(ContextCompat.getColor(context, R.color.colorMapItem));
        }
        try {
            FlightStatusBar item = list.get(i);
            viewHolder.tv_title_name.setText(item.getTitle());

            StringBuilder sb = new StringBuilder();
            sb.append(item.getContent());
            if (!TextUtils.isEmpty(item.getExtendContent()) /*&& !item.getTitle().equals(FlightStatusBar.YDD.getTitle())*/) {
                sb.append(" ");
                sb.append(item.getExtendContent());
            }
            viewHolder.tv_title_name_show.setText(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (onItemClickListener != null) {
                    int pos = viewHolder.getLayoutPosition();
                    onItemClickListener.onItemClick(list.get(i), pos, viewTag);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return null == list ? 0 : list.size();
    }

    public class TemplateViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_title_name_show, tv_title_name;

        public TemplateViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_title_name = itemView.findViewById(R.id.tv_title_name);
            tv_title_name_show = itemView.findViewById(R.id.tv_title_name_show);
        }
    }


}
